﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;
using BLL.Exceptions;
using MessageBoxUtils;


namespace CorporateGymManagementSystem
{
    public delegate void RegisterArrivalEventHandler();

    

    /// <summary>
    /// Interaction logic for searchTraineeWindow.xaml
    /// </summary>
    public partial class SearchTraineeWindow : Window
    {
        public event RegisterArrivalEventHandler RegisterArrival;

        public SearchTraineeWindow()
        {
            InitializeComponent();
            Filter();
            toggleTraineeManagementButtons();
        }

        private void searchTraineeTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            Filter();
        }

        private void Filter()
        {
            string search = searchTraineeTextBox.Text.Trim();
            List<TraineeDataGridItem> list =
                (from trainee in TraineeManager.Instance.GetTraineesByName(search)
                 select new TraineeDataGridItem { Trainee = trainee, MedicalApproval = MedicalApprovalManager.Instance.GetMedicalApprovalById(trainee.MedicalApprovalId)  }).ToList();
            DataContext = list;
        }

        private void updateMedicalApprovalButton_Click(object sender, RoutedEventArgs e)
        {
            TraineeDataGridItem selectedItem = (traineeList.SelectedItem as TraineeDataGridItem);
            if (selectedItem == null)
            {
                return;
            }

            Trainee selectedTrainee = selectedItem.Trainee;
            int medicalApprovalId = selectedTrainee.MedicalApprovalId;
            MedicalApproval mediaclApproval = MedicalApprovalManager.Instance.GetMedicalApprovalById(medicalApprovalId);
            UpdateTraineeMedicalApprovalWindow updateMedicalApprovalWindow = new UpdateTraineeMedicalApprovalWindow(mediaclApproval);
            updateMedicalApprovalWindow.MedicalApprovalUpdated += Filter;
            updateMedicalApprovalWindow.ShowDialog();
            
        }

        private void EditTraineeButton_Click(object sender, RoutedEventArgs e)
        {
            TraineeDataGridItem selectedItem = (traineeList.SelectedItem as TraineeDataGridItem);
            if (selectedItem == null)
            {
                return;
            }
            Trainee selectedTrainee = selectedItem.Trainee;
            NewTraineeCreationWindow editTraineeWindow = new NewTraineeCreationWindow(selectedTrainee);
            editTraineeWindow.NewTraineeAdded += Filter;
            editTraineeWindow.ShowDialog();
        }

        private void registerArrivalButton_Click(object sender, RoutedEventArgs e)
        {
            TraineeDataGridItem selectedItem = (traineeList.SelectedItem as TraineeDataGridItem);
            if (selectedItem == null)
            {
                statusBar.Foreground = Brushes.Crimson;
                statusBar.Content = "You must select A trainee!!";
                return;
            }

            bool success = false;
            Trainee selectedTrainee = selectedItem.Trainee;
            try
            {
                TraineeManager.Instance.MarkAsVisit(selectedTrainee);

                //statusBar.Foreground = Brushes.Green;
                //statusBar.Content = "Trainee Registered!!";
                RegisterArrival();

            }
            catch (VisitedTodayException ex)
            {
                MessageBoxResult result = WPFMessageBox.Show("Trainee already registered for today", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            catch (MedicalApprovalExpiredException ex)
            {
                MessageBoxResult result = WPFMessageBox.Show("Medical approval expired", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning);
                //statusBar.Foreground = Brushes.Crimson;
                //statusBar.Content = "An Error Has Occured!!";
            }
            catch (PaymentException ex)
            {
                MessageBoxResult result = WPFMessageBox.Show("No Payement for current month", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning);
                //statusBar.Foreground = Brushes.Crimson;
                //statusBar.Content = "An Error Has Occured!!";
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.StackTrace.ToString());
                //statusBar.Foreground = Brushes.Crimson;
                //statusBar.Content = "An Error Has Occured!!";
            }
        }

        private void removeTraineeButton_Click(object sender, RoutedEventArgs e)
        {
            TraineeDataGridItem selectedItem = (traineeList.SelectedItem as TraineeDataGridItem);
            if (selectedItem == null)
            {
                return;
            }

            Trainee selectedTrainee = selectedItem.Trainee;
            if (WPFMessageBox.Show("Are you sure you want to remove this trainee?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            { 
                TraineeManager.Instance.RemoveTrainee(selectedTrainee);
            }
            Filter();
        }

        private void addNewTrainee_Click(object sender, RoutedEventArgs e)
        {
            NewTraineeCreationWindow newTraineeCreationWindow = new NewTraineeCreationWindow();
            newTraineeCreationWindow.NewTraineeAdded += Filter;
            newTraineeCreationWindow.ShowDialog();
            Filter();
        }

        private void paymentsButton_Click(object sender, RoutedEventArgs e)
        {
            TraineeDataGridItem selectedItem = (traineeList.SelectedItem as TraineeDataGridItem);
            if (selectedItem == null)
            {
                return;
            }

            Trainee selectedTrainee = selectedItem.Trainee;
            PaymentsWindow window = new PaymentsWindow(selectedTrainee);
            window.ShowDialog();
            Filter();
        }

        private void traineeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            toggleTraineeManagementButtons();
        }

        private void toggleTraineeManagementButtons()
        {
            if (traineeList.SelectedItem == null)
            {
                registerArrivalButton.IsEnabled = false;
                EditTraineeButton.IsEnabled = false;
                removeTraineeButton.IsEnabled = false;
                updateMedicalApprovalButton.IsEnabled = false;
                paymentsButton.IsEnabled = false;
            }
            else
            {
                registerArrivalButton.IsEnabled = true;
                EditTraineeButton.IsEnabled = true;
                removeTraineeButton.IsEnabled = true;
                updateMedicalApprovalButton.IsEnabled = true;
                paymentsButton.IsEnabled = true;
            }
        }

        private void traineeList_LostFocus(object sender, RoutedEventArgs e)
        {
            
        }
    }

    public class TraineeDataGridItem
    {
        public Trainee Trainee { get; set; }
        public MedicalApproval MedicalApproval { get; set; }
        public string WorkerId { get { return Trainee.WorkerId; } set { } }
        public string FirstName { get { return Trainee.FirstName; } set { } }
        public string LastName { get { return Trainee.LastName; } set { } }
        public string Email { get { return Trainee.Email; } set { } }
        public string Phone { get { return Trainee.Phone; } set { } }
        public string ExpirationDate { get { return MedicalApproval.ExpirationDate.ToShortDateString(); } set { } }
    }
}
